/********************************************************
 Name          : file_system.h
 Author        : Khachatur Gyozalyan
 Copyright     : Copyright by Antel Systems
 Description   :
 **********************************************************/
#ifndef FILE_SYSTEM_H_
#define FILE_SYSTEM_H_

#include "string.h"
#include "at25dfx.h"
#include "flashc.h"

#define MAX_FILE_COUNT    10

#define CBTB_FILE_INDEX   7
#define FCFG_FILE_INDEX   8
#define UCFG_FILE_INDEX   9

#define CBTB_Available 0
#define FCFG_Available 1
#define UCFG_Available 2



#define INTERNAL_USER_CFG_FILE_ADDRESS   AVR32_FLASH_ADDRESS + AVR32_FLASH_SIZE - 0x8000


struct file_t
{
	char name[10];
	unsigned int sector_number;
	unsigned int max_size;
	int file_exist;
	unsigned char file_type;
};

extern const struct file_t files_list[MAX_FILE_COUNT];

#define FCFG_FILE_TYPE   0x40
#define UCFG_FILE_TYPE   0x50
#define CBTB_FILE_TYPE   0x60

#define IMAGE_FILE_TYPE  0x80

#define SIZEOF_FILEHEADER   32

unsigned char StartupMode;
#define START_UP_NORMAL				0
#define START_UP_LOAD_DEFAULT		1
#define START_UP_REQUIRE_WRITE		2

unsigned char AvailableFiles;
#define USER_CONFIG_IS_AVALIABLE		((AvailableFiles >> UCFG_Available) & 1)
#define FACTORY_CONFIG_IS_AVALIABLE		((AvailableFiles >> FCFG_Available) & 1)
#define CALIB_TABLE_IS_AVALIABLE		((AvailableFiles >> CBTB_Available) & 1)

struct FileHeader
{
	unsigned char   Type;
    unsigned char   Major;
    unsigned char   Minor;
    unsigned char   Rev;
    unsigned char	DeviceID[16];
    unsigned char   Reserved[8];

    unsigned char Size[3];

	unsigned char crc8;
};

int find_file(struct file_t *file,char * name);
int check_file_header(unsigned char * ptr,struct file_t file);
int copy_from_externalflash_to_internalflash(unsigned int rom_address,unsigned int flash_address,unsigned int byte_count);
void read_from_internalflash(int address, int size, unsigned char* data);
void loading_config(int boot_option);
unsigned char check_files_availability(void);
unsigned char save_ucfg(void);
void organize_files_operation(void);
#endif /* FILE_SYSTEM_H_ */